package com.lagou.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class UserClientHandler2 extends ChannelInboundHandlerAdapter implements Callable {

    //1、定义成员变量
    private ChannelHandlerContext context;
    private Object result;
    private Object param;

    //2、实现channelActive 客户端和服务器连接时，该方法就自动执行

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.context = ctx;
    }


    //3、实现channelRead 当我们读到服务器数据，方法自动执行

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        result = msg.toString();
        notify();
    }


    //4、将客户端数据写到服务端

    public synchronized Object call() throws Exception {
        context.writeAndFlush(param);
        wait();
        return result;
    }



    public void setParam(Object param){
        this.param = param;
    }
}
